﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Mvc;

using $prjsn$.Entity.Services;
using $prjsn$.Data.Services.Xml;


namespace $safeprojectname$.Dao.Xml
{
    [TestClass]
    public class OrderByTest : TestBase
    {
        [TestMethod]
        public void TestXmlOrderBy()
        {
            var dao = new ArticleDao();
            dao.ArticleCategoryDao = new Data.Services.Xml.ArticleCategoryDao();
            dao.ArticleGroupDao = new Data.Services.Xml.ArticleGroupDao();

            var path = "ArticleGroup.Code";
            var filter = new ArticleFilter();
            filter.OrderBy[path] = true;
            filter.OrderBy["ID"] = false;

            var list = dao.Find(filter);

            Assert.IsTrue(list.First().ArticleGroupCode == "Any"); // Home Article has Any

            var docs = list.Where(a => a.ArticleGroup.Code != "Any").ToList();

            Assert.IsTrue(docs.Count > 1);
            Assert.IsTrue(docs[1].ID < docs[0].ID);

        }
        [TestMethod]
        public void TestOrderByBuilder()
        {
            var a = new Article { Title = "TestTitle" };
            var ag = new $prjsn$.Entity.Services.ArticleGroup { Code = "Test" };

            a.ArticleGroup = ag;

            var code = a.BuildMember("TitleXXX");
            Assert.IsTrue(code.IsNull());

            code = a.BuildMember("Title");
            Assert.IsTrue(code.ToString() == "TestTitle");

            var group = a.BuildMember("ArticleGroup.CodeXXX");
            Assert.IsTrue(group.IsNull());

            group = a.BuildMember("ArticleGroup.Code");
            Assert.IsTrue(group.ToString() == "Test");
        }


    }
}
